#!/usr/bin/python

from sklearn import svm
from sklearn import preprocessing
import numpy
import sys
import pickle
import talib

execfile('/opt/python/learning/sampler.py')
execfile('/opt/python/includes/finance.py')

#selsvm=svm.SVC(kernel='poly',C=10000,verbose=True,degree=3)
#buysvm=svm.SVC(kernel='poly',C=10000,verbose=True,degree=3)
selsvm=svm.SVC(kernel='rbf',gamma=float(sys.argv[4]),C=float(sys.argv[3]))
buysvm=svm.SVC(kernel='rbf',gamma=float(sys.argv[4]),C=float(sys.argv[3]))

index=sys.argv[1]
saveto=sys.argv[2]

inputs={}
X=[]
T=[]
ybuy=[]
tbuy=[]
ysel=[]
tsel=[]
prices=getOHLCDict(index)
date=prices['date']
o=numpy.array(prices['open'])
h=numpy.array(prices['high'])
l=numpy.array(prices['low'])
c=numpy.array(prices['close'])
prices['cci']=talib.CCI(h,l,c,3)
prices['c_macd'], prices['c_macdsig'], prices['c_macdhist'] = talib.MACD(c,3,6,4)
prices['o_macd'], prices['o_macdsig'], prices['o_macdhist'] = talib.MACD(o,3,6,4)
prices['c_stoc_k'], prices['c_stoc_d'] = talib.STOCH(h,l,c)
prices['rsi3']=talib.RSI(c,3)
prices['fsrsi'], prices['srsi']=talib.STOCHRSI(c,3)
prices['max5']=talib.MAX(h,5)
prices['max14']=talib.MAX(h,14)
prices['low5']=talib.MIN(l,5)
prices['low14']=talib.MIN(l,14)

for i in range(30, len(prices['date'])-1):
    inputs[date[i]]=getSample_deltas(prices,i)

for i in range(len(prices['date'])):
    if not date[i] in inputs.keys(): continue
    if(i%10!=0):
        ybuy.append(getResult_nextDay_buy(prices,i))
    else:
        tbuy.append(getResult_nextDay_buy(prices,i))
    if(i%10!=0):
        ysel.append(getResult_nextDay_sell(prices,i))
    else:
        tsel.append(getResult_nextDay_sell(prices,i))
    if(i%10!=0): X.append(inputs[date[i]])
    else: T.append(inputs[date[i]])
    if i == 0: continue

T=numpy.array(T)
X=numpy.array(X)
X_norm=preprocessing.scale(X)
T_norm=preprocessing.scale(T)
selsvm.fit(X_norm,numpy.array(ysel))
pickle.dump(selsvm,open(saveto+"-sel","wb"))
buysvm.fit(X_norm,numpy.array(ybuy))
pickle.dump(buysvm,open(saveto+"-buy","wb"))

correct=0
fail=0
for i in range(len(X_norm)):
    sellPrediction=selsvm.predict(X_norm[i])
    buyPrediction=buysvm.predict(X_norm[i])

    if(ybuy[i]==1 and sellPrediction==0 and buyPrediction==1): correct=correct+1
    if(ysel[i]==1 and sellPrediction==1 and buyPrediction==0): correct=correct+1

    if(ybuy[i]==1 and sellPrediction==1 and buyPrediction==0): fail=fail+1
    if(ysel[i]==1 and sellPrediction==0 and buyPrediction==1): fail=fail+1
    #print "Buy: "+str(ybuy[i])+". Prediction: "+str(buyPrediction)+". Sell: "+str(ysel[i])+". Prediction: "+str(sellPrediction)+" "+str(fail)+" failed and "+str(correct)+" correct predictions"

print "In-Sample: After "+str(i+1)+" predictions, "+str(correct)+" were correct and "+str(fail)+" were incorrect: "+str(correct*100/(correct+fail))+"%"
in_pct=correct*100/(correct+fail)
  
correct=0
fail=0
for i in range(len(T_norm)):
    sellPrediction=selsvm.predict(T_norm[i])
    buyPrediction=buysvm.predict(T_norm[i])

    if(tbuy[i]==1 and sellPrediction==0 and buyPrediction==1): correct=correct+1
    if(tsel[i]==1 and sellPrediction==1 and buyPrediction==0): correct=correct+1

    if(tbuy[i]==1 and sellPrediction==1 and buyPrediction==0): fail=fail+1
    if(tsel[i]==1 and sellPrediction==0 and buyPrediction==1): fail=fail+1
    #print "Buy: "+str(tbuy[i])+". Prediction: "+str(buyPrediction)+". Sell: "+str(tsel[i])+". Prediction: "+str(sellPrediction)+" "+str(fail)+" failed and "+str(correct)+" correct predictions"

print "Out-Sample: After "+str(i+1)+" predictions, "+str(correct)+" were correct and "+str(fail)+" were incorrect: "+str(correct*100/(correct+fail))+"%"
out_pct=correct*100/(correct+fail)
f=open('results.csv','a')
f.write(str(sys.argv[3])+","+str(sys.argv[4])+","+str(in_pct)+","+str(out_pct)+"\n")
f.close()
